package cn.com.api.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.api.entity.ApiU_message;
import cn.com.api.entity.ApiU_user_friend;
import cn.com.api.entity.ApiU_user_friend_group;
import cn.com.api.entity.ApiU_user_friend_group_detail;
import cn.com.api.mapper.ApiU_messageMapper;
import cn.com.api.mapper.ApiU_user_friendMapper;
import cn.com.api.mapper.ApiU_user_friend_groupMapper;
import cn.com.api.mapper.ApiU_user_friend_group_detailMapper;
import cn.com.api.service.ApiU_user_friendService;
import cn.com.api.service.impl.base.U_user_friendServiceImplBase;
import cn.com.util.PageInfo;
import cn.com.util.Util;
import cn.com.util.Utils;

/**
 * 
 **/
@Service
public class ApiU_user_friendServiceImpl extends U_user_friendServiceImplBase implements ApiU_user_friendService {
	@Autowired
	private ApiU_user_friendMapper apiU_user_friendMapper;
	@Autowired
	private ApiU_user_friend_groupMapper apiU_user_friend_groupMapper;
	@Autowired
	private ApiU_user_friend_group_detailMapper apiU_user_friend_group_detailMapper;
	@Autowired
	private ApiU_messageMapper apiU_messageMapper;

	@Override
	public Integer checkUserFriendState(String user_id, String friend_id) {
		// 0可加好友， 1申请中， 2已是好友， 3已拒绝， 4黑名单
		Integer ret = 0;
		ApiU_user_friend user_friend = this.apiU_user_friendMapper.checkUserFriendState(user_id, friend_id);
		if (user_friend == null) {
			ret = 0;
		} else {
			ret = user_friend.getStatus() + 1;
		}

		return ret;
	}

	@Override
	public ApiU_user_friend getUserFriendState(String user_id, String friend_id) {
		// 0可加好友， 1申请中， 2已是好友， 3已拒绝， 4黑名单

		return this.apiU_user_friendMapper.checkUserFriendState(user_id, friend_id);

	}

	@Override
	public ApiU_user_friend getUserFriend(String user_id, String friend_id) {
		return this.apiU_user_friendMapper.checkUserFriendState(user_id, friend_id);
	}

	@Override
	public int U_user_friendInsertion(ApiU_user_friend user_friend) {
		return this.apiU_user_friendMapper.U_user_friendInsertion(user_friend);
	}

	// 根据Map查询，Status=1好友，Status=2企业主好友， Status=3黑名单
	@Override
	public List<ApiU_user_friend> findU_user_friendByMap(Map param, PageInfo pageinfo) {
		List<ApiU_user_friend> user_friends = this.apiU_user_friendMapper.findU_user_friendByMap(param);
		// 缩略图
		for (ApiU_user_friend friend : user_friends) {
			if (!Util.isNull(friend.getFriend_head()) && friend.getFriend_head().length() > 0) {
				String friend_header = friend.getFriend_head() + "?imageView2/1/w/100/h/100";
				friend.setFriend_head(friend_header);
			}
		}

		return user_friends;
	}

	// 获取用户好友关系记录
	public ApiU_user_friend findUser_friend_Record(Map param) {
		return this.apiU_user_friendMapper.getUserFriendRecord(param);
	}

	// 获取用户黑名单
	public List<ApiU_user_friend> findUser_friend_Black_List(Map param) {
		List<ApiU_user_friend> friend_list = this.apiU_user_friendMapper.findU_user_friendByMap(param);
		List<ApiU_user_friend> new_friend_list = new ArrayList<ApiU_user_friend>();
		// 处理谁加谁进了黑名单， 只取主动加的
		for (ApiU_user_friend user_friend : friend_list) {
			// Ext1:谁把谁加入了黑名单：user_id, friend_id, both两人都加了
			if (user_friend.getExt1().equals("both")) {
				new_friend_list.add(user_friend);
			} else if (user_friend.getExt1().equals(param.get("user_id").toString())) {
				new_friend_list.add(user_friend);
			}
		}
		return new_friend_list;
	}

	// 删除好友
	@Transactional
	public Map<String, Object> delFriendRelation(String user_id, String friend_id) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		// 0可加好友， 1申请中， 2已是好友， 3已拒绝， 4黑名单
		Integer ret = 0;
		ApiU_user_friend user_friend = this.apiU_user_friendMapper.checkUserFriendState(user_id, friend_id);
		if (user_friend == null) {
			ret = 0;
		} else {
			ret = user_friend.getStatus() + 1;
		}
		if (ret == 2 || ret == 4) {
			// 删除好友关系
			apiU_user_friendMapper.deleteU_user_friend(user_friend.getId());
			// 删除消息组数据，双方的都要删除
			ApiU_user_friend_group_detail detail = apiU_user_friend_group_detailMapper
					.findFriendGroupDetailByFriend(user_id, friend_id, 0);
			if (detail != null) {
				apiU_user_friend_group_detailMapper.deleteU_user_friend_group_detail(detail.getId());
			}
			ApiU_user_friend_group_detail detail2 = apiU_user_friend_group_detailMapper
					.findFriendGroupDetailByFriend(friend_id, user_id, 0);
			if (detail2 != null) {
				apiU_user_friend_group_detailMapper.deleteU_user_friend_group_detail(detail2.getId());
			}
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功");
		} else {
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "不是好友");
		}
		return map;
	}

	// 同意驳回好友请求
	@Transactional
	public Map<String, Object> acceptfriendRequest(String user_id, String conn_id, String flag) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		ApiU_user_friend ufriend = apiU_user_friendMapper.findU_user_friendById(Integer.valueOf(conn_id));
		if (ufriend != null) {
			String msg = "";
			ufriend.setUpdate_time(Util.CurrentTime());
			ufriend.setStatus(Integer.valueOf(flag));
			if (flag.equals("1")) {// 同意
				msg = ufriend.getF_rename() + " 通过了您的好友请求！";
				// 添加到默认分组(我的消息)
				// 加入自己的分组
				String req_friend_id = ufriend.getFriend_id();
				String req_user_id = ufriend.getUser_id();
				if (user_id.equals(req_friend_id)) {
					req_friend_id = ufriend.getUser_id();
					req_user_id = ufriend.getFriend_id();
				}
				ApiU_user_friend_group f_group = apiU_user_friend_groupMapper.findFriend_groupByName(req_user_id,
						"我的消息");
				ApiU_user_friend_group_detail f_group_detail = new ApiU_user_friend_group_detail();
				f_group_detail.setFriend_group_id(f_group.getId().toString());
				f_group_detail.setAdd_time(Util.CurrentTime());
				f_group_detail.setFriend_id(req_friend_id);
				f_group_detail.setType(0);
				apiU_user_friend_group_detailMapper.U_user_friend_group_detailInsertion(f_group_detail);
				// 加入好友的分组
				ApiU_user_friend_group f_group2 = apiU_user_friend_groupMapper.findFriend_groupByName(req_friend_id,
						"我的消息");
				ApiU_user_friend_group_detail f_group_detail2 = new ApiU_user_friend_group_detail();
				f_group_detail2.setFriend_group_id(f_group2.getId().toString());
				f_group_detail2.setAdd_time(Util.CurrentTime());
				f_group_detail2.setFriend_id(req_user_id);
				f_group_detail2.setType(0);
				apiU_user_friend_group_detailMapper.U_user_friend_group_detailInsertion(f_group_detail2);
			} else { // 忽略
				msg = ufriend.getF_rename() + " 拒绝了您的好友请求！";
			}
			apiU_user_friendMapper.updateU_user_friend(ufriend);
			// 发系统消息通知对方
			ApiU_message message = new ApiU_message();
			// message.setConn_id(ufriend.getId());
			message.setType(1);
			message.setAdd_time(Util.CurrentTime());
			message.setIs_read(0);
			message.setUser_id(ufriend.getUser_id());
			message.setTitle("请求好友");
			message.setContent(msg);
			apiU_messageMapper.U_messageInsertion(message);

			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "操作完成！");
			map.put(Utils.DATAS, ufriend);
		} else {
			map.put(Utils.RESULT, Utils.RESULT_RC_PARAMTER_ERROR);
			map.put(Utils.MESSAGE, "参数异常！");
		}
		return map;
	}
}
